package org.mathcloud.math.core.scilab.variable;

import org.mathcloud.math.core.common.function.FunctionObject;
import org.mathcloud.math.core.common.quantity.QuantityObject;
import org.mathcloud.math.core.common.signature.VariableObjectSignature;
import org.mathcloud.math.core.common.variable.VariableObject;
import org.mathcloud.math.core.scilab.factory.ScilabQuantityObjectFactory;
import org.mathcloud.math.core.scilab.function.ScilabFunction;
import org.mathcloud.math.core.scilab.manager.ScilabSupervisor;
import org.mathcloud.math.core.scilab.quantity.ScilabQuantityObject;
import org.scilab.modules.javasci.Scilab;

public class ScilabVariable implements VariableObject
{
	public ScilabVariable()
	{
		sciEngine = ScilabSupervisor.getScilabEngine();
		sciVarName = "scilab_var_"+ScilabSupervisor.getVarId();
	}

	@Override
	public void assign(QuantityObject arg0)
	{
		quantity = ScilabQuantityObjectFactory.getInstance(arg0);		
		if (quantity!=null)
		{
			String sciJob;
			sciJob = sciVarName +"="+quantity.getVarName();			
			sciEngine.exec(sciJob);
		}
	}

	@Override
	public void assign(VariableObject arg0)
	{		
		refVariable = arg0;		
	}
	@Override
	public void assign(FunctionObject arg0)
	{
		// TODO Auto-generated method stub		
	}

	@Override
	public QuantityObject getQuantity()
	{
		if (quantity!=null)
			return quantity;
		else if (function!=null)
			return function.getQuantityValue();
		else if (refVariable!=null)
			return refVariable.getQuantity();
		else return null;
	}

	
	@Override
	public VariableObjectSignature getSignature()
	{
		return signature;
	}
	
	private Scilab sciEngine;
	private String sciVarName;
	protected VariableObjectSignature signature;
	protected ScilabQuantityObject quantity;
	protected ScilabFunction function;
	protected VariableObject refVariable;

}
